const actions = {
  'player.add': (player, package) => {
    let username = package.data.username;
    let hasUsername = global.Core.player.playerUsername.indexOf(username) >= 0;
    if (!username || username.length > 6 || global.Core.util.trim(username) == "" || hasUsername) {
      global.Core.socket.system(hasUsername ? '名称已经存在了, 换一个吧' : `名称不合格!!!又是谁在搞事情???`, 'red', player.socket);
      player.emit('player.add.error', {
        error: 'username'
      });
      // 给player加上无效的状态
      player.status = false;
      return;
    }

    global.Core.player.playerUsername.push(username);
    global.Core.socket.system(`${username}连接上了游戏, 欢迎欢迎~`, 'green');
    global.Core.util.objMap(package.data, (k, v) => {
      player.set(k, v);
    });
    player.status = true;
  },

  'room.create': (player, package) => {
    global.Core.room.create(player, package.data.status);
  },
  'room.add': (player, package) => {
    global.Core.room.add(package.data.key, player);
  },
  'room.leave': (player, package) => {
    if (!player.data.room) {
      // 如果玩家不在任何房间中
      global.Core.socket.system(`你不在任何房间, 不要搞事情`, 'red', player.socket);
      global.Core.socket.emit('room.leave.error');
      return;
    }

    global.Core.room.leave(player);
  },
  'room.shit': (player, package) => {
    // 找到这个玩家
    let shitPlayer = global.Core.player.get(package.data.id);
    // 获取房间
    let room = global.Core.room.get(player.data.room);
    if (!shitPlayer || !room) {
      // 玩家不存在？？？
      // 房间不存在？？？
      return;
    }
    // 判断是否有权限
    if (room.master != player.socket.id) {
      global.Core.socket.system(`你没有权限踢别人...，在这么搞我就要报警了`, 'red', player.socket);
      return;
    }

    global.Core.room.leave(shitPlayer, true);
    global.Core.socket.system('你被房主嫌弃了，哈哈哈哈哈', 'red', shitPlayer.socket);
    global.Core.socket.system(`房主踢掉了${shitPlayer.data.username}`, 'yellow', null, player.data.room);
  },
  'room.list': (player, package) => {
    let result = [];
    Object.keys(global.Core.room.roomList).forEach(key => {
      let item = global.Core.room.roomList[key];
      if (item.status == 1) result.push(item);
    })
    player.emit('room.list', result);
  },
  'room.ready': (player, package) => {
    player.set('ready', package.data.ready);
    global.Core.room.sendRoomInfo(player.data.room);
  },

  'game.create': (player, package) => {
    let room = global.Core.room.get(player.data.room);

    if(!room){
      global.Core.room.destroy(player.data.room);
      return;
    }

    // 判断是否为房主
    if(room.master != player.socket.id){
      global.Core.socket.system('你不是房主，不能开始游戏（网站的？？搞事情？？？我打不死你）', 'red', player.socket);
      return;
    }

    // 判断玩家房间所有人是否准备
    for (let i = 0; i < room.playerList.length; i++) {
      let item = room.playerList[i];
      if (!item.ready && item.id != room.master) {
        // 有玩家没有准备
        global.Core.socket.system('还有玩家没有准备，快打ta。', 'red', null, player.data.room);
        return;
      }
    }

    // 告诉房间内所有人，我创建游戏了
    global.Core.socket.emit('game.create', null, null, player.data.room);

    // 创建一个游戏实例
    global.Core.game.create(room);

    // 告诉房间，游戏创建完毕
    global.Core.socket.emit('game.create.end', null, null, player.data.room);
  },
  'game.action': (player, package) => {
    global.Core.game.action(player, package.data.action);
  },

  'message': (player, package) => {
    let type = package.data.type;

    if (!player.data.username) {
      return;
    }

    if (type == 'global') {
      global.Core.socket.emit('message', {
        msg: package.data.msg,
        name: player.data.username,
        type: 'player',
        area: '全服'
      });
    } else {
      if (!player.data.room) {
        player.emit('message', {
          msg: '你不在任何一个房间中...，给朕退下！',
          type: 'system',
          color: 'red'
        });
        return;
      }
      global.Core.socket.emit('message', {
        msg: package.data.msg,
        name: player.data.username,
        type: 'player',
        area: '队伍'
      }, null, player.data.room);
    }
  }
}

module.exports = function (data) {
  if (!data.action) {
    console.warn('无法处理的数据包', data);
    return;
  }
  let action = actions[data.action];
  if (!action) {
    console.warn('无法处理的动作: ' + data.action);
    return;
  }
  actions[data.action](this, data);
}